// lives/admin/cash/cash.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected: -1,
    mobile: '',
    name: '',
    bank_name: '',
    cash: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().page.onLoad(this, options)
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  getInfo: function () {
    wx.showLoading({
      title: '加载中...',
    })
    getApp().request({
      url: getApp().api.live.get_cash_info,
      success: data => {
          if (data.code == 0) {
              this.setData(data.data);
          }
      },
      complete: () => {
          wx.hideLoading()
      }
    })
  },

  select: function (e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      selected: index,
      name: '',
      mobile: '',
      bank_name: ''
    })
  },

  onInput: function (e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      [name]: e.detail.value
    })
  },

  onSubmit: function () {
    if (this.data.cash_max_day != -1 && this.data.cash > this.data.cash_max_day) {
      wx.showToast({
          title: "提现金额不能超过" + this.data.cash_max_day + "元",
          icon: "none"
      })
      return
    }
    if (this.data.cash < this.data.min_cash_price) {
      wx.showToast({
          title: "提现金额不能低于" + this.data.min_cash_price + "元",
          icon: "none"
      })
      return
    }
    if (this.data.selected == -1) {
      wx.showToast({
          title: "请选择提现方式",
          icon: "none"
      })
      return
    }
    var type = this.data.cash_method[this.data.selected].key
    if (type == 'wechat' || type == 'alipay' || type == 'bank') {
      if (this.data.name == '') {
        wx.showToast({
            title:  (type == 'bank' ? '开户人' : '姓名') + "不能为空",
            icon: "none"
        })
        return
      }
      if (this.data.mobile == '') {
        wx.showToast({
            title: "账号不能为空",
            icon: "none"
        })
        return
      }
      if (type == 'bank' && this.data.bank_name == '') {
        wx.showToast({
            title: "开户行不能为空",
            icon: "none"
        })
        return
      }
    }
    wx.showLoading({
      title: '加载中...',
    })
    getApp().request({
      url: getApp().api.live.cash_apply,
      method: "POST",
      data: {
        cash: this.data.cash,
        name: this.data.name,
        mobile: this.data.mobile,
        bank_name: this.data.bank_name,
        pay_type: this.data.cash_method[this.data.selected].key
      },
      success: res => {
          if (res.code == 0) {
            wx.showToast({
              title: '提交成功',
              icon: "none"
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          } else {
            wx.showToast({
                title: res.msg,
                icon: "none"
            })
          }
      },
      complete: () => {
          wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})